﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.IO;

using FL.DBModule;
using FL.DBModule.Sys;
using FL.SysEntity;
using FL.Common;

namespace FL.Bussiness.Sys
{
    public partial class BFucntion : BaseBussnies
    {
        class OpertionItems
        {
            public string area
            {
                get;
                set;
            }

            public string item
            {
                get;
                set;
            }
        }


        /// <summary>
        /// 生成按钮HTML
        /// </summary>
        /// <param name="code"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public string GenerateButtonHTML(string code, Guid UserID)
        {
            string functionid = code;
            string BtnItems = "";
            string BtnEvents = "";
            string InitBtnEvents = "";
            IList<OpertionItems> opertionItems = new List<OpertionItems>();
            StringBuilder strHTML = new StringBuilder("");

            if (code.Length != 36)
            {
                string menu_code = code;
                t_sys_function _function = this.GetEntityByCondition<t_sys_function>(string.Format("code='{0}'", menu_code));
                functionid = _function == null ? "" : _function.id.ToString();
            }

            v_user_function_operation function = (new BaseBussnies(new v_user_function_operation())).GetEntityByCondition("function_id='" + functionid + "' and user_id='" + UserID.ToString() + "'") as v_user_function_operation;
            string userID = UserID.ToString();
            if (function == null)
            {
                BtnItems = "";
                BtnEvents = "";
                InitBtnEvents = "";
            }
            else
            {
                string[] btns = function.names.Split(',');
                string[] btnTexts = function.captions.Split(',');
                string[] fns = function.actions.Split(',');
                string[] barItems = function.BarItems.Split(',');
                string[] css = function.iconcss.Split(',');
                string[] area = function.places.Split(',');
                if (barItems == null)
                {
                    BtnItems = "";
                    BtnEvents = "";
                    InitBtnEvents = "";
                    return "";
                }
                if (barItems.Count() == 0)
                {
                    BtnItems = "";
                    BtnEvents = "";
                    InitBtnEvents = "";
                    return "";
                }
                //生成按钮
               
                for (int i = 0; i < btns.Length; i++)
                {
                    foreach (string item in barItems)
                    {

                        if (item == btns[i])
                        {
                            string strFormater = "<a class=\"mini-button\"  iconcls=\"{0}\" plain=\"true\"  onclick=\"{1}\" id=\"{3}\" place=\"{4}\">{2}</a>";
                            string strItem = string.Format(strFormater, css[i], fns[i], btnTexts[i], btns[i], area[i]);
                            if (opertionItems.Where(p => p.item == strItem).Count() == 0)
                            {
                                strHTML.Append(strItem);
                                opertionItems.Add(new OpertionItems { area = area[i], item = strItem });
                            }
                            break;
                        }
                    }
                }
            }
            return strHTML.ToString();
        }
      
    }
}
